package org.exia.minisig.swing.view.navigation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URI;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import org.exia.minisig.model.object.DescriptibleElement;
import org.exia.minisig.model.object.Itinerary;
import org.exia.minisig.model.object.News;
import org.exia.minisig.model.object.Place;
import org.exia.minisig.swing.controller.GUIManager;
import org.exia.minisig.swing.controller.HomeFramMode;
import org.exia.minisig.swing.view.common.ImageNavigation;
import org.exia.minisig.swing.view.home.HomeFram;

@SuppressWarnings("serial")
public class InterestInfoPanel extends JPanel {
	//VAR
	private DescriptibleElement element;
	private NavigationFram parent;
	//CONSTRUTOR
	public InterestInfoPanel(DescriptibleElement _element, NavigationFram _parent){
		initializeVar(_element, _parent);
	}
	public void initializeVar(DescriptibleElement _element, NavigationFram _parent){
		this.setBackground(Color.white);
		this.parent = _parent;
		this.element = _element;
		if (this.parent.getMode() ==  HomeFramMode.PLACE){
			fillPanelMap();
		} else {
			fillPanel();
		}
	}
	
	public void fillPanel(){
		this.removeAll();
		this.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
		this.setLayout(new GridBagLayout());
		GridBagConstraints gc = new GridBagConstraints();
		gc.fill=GridBagConstraints.BOTH;
		//PARAMETER
		gc.weightx=1;
		gc.weighty=1;
		//---------------------------------------//
		//LABEL
		gc.gridheight=1;
		gc.gridwidth= 4;
		gc.gridx=0;
		gc.gridy=0;
		gc.insets = new Insets(0, 0, 5, 0);
		JPanel panelTitre = new JPanel(new BorderLayout());
		
		JLabel titre = new JLabel(element.getLabel());
		
		panelTitre.setBackground(Color.white);
		titre.setBackground(Color.white);
		titre.setOpaque(true);
		
		titre.setVerticalAlignment(SwingConstants.CENTER);
		titre.setHorizontalAlignment(SwingConstants.CENTER);
		panelTitre.add(titre, BorderLayout.CENTER);
		this.add(panelTitre, gc);
		//--------------------------------RDC------------------------------//
		//ETAGE 01
		//BTN+
		gc.gridwidth = 4;
		gc.gridheight = 1;
		gc.gridx=0;
		gc.gridy=1;
		gc.insets = new Insets(0, 5, 0, 5);
		ImageNavigation nav = new ImageNavigation(this.element);
		this.add(nav, gc);
		
		
		//ETAGE DESCRIPTION
		gc.insets = new Insets(3, 5, 3, 5);
		gc.gridwidth = 4;
		gc.gridheight = 2;
		gc.gridx=0;
		gc.gridy=2;
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		JLabel description = new JLabel(element.getText());
		JScrollPane scroll = new JScrollPane(description);
		panel.add(scroll, BorderLayout.CENTER);
		this.add(panel, gc);
		
		//ETAGE URLS 
		gc.gridwidth = 4;
		gc.gridheight = 3;
		gc.gridx=0;
		gc.gridy=5;
		gc.weighty = 2;
		final JList<String> listeURL = new JList<>(element.getUrls().toArray(new String[element.getUrls().size()]));
		listeURL.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
			}
			@Override
			public void mousePressed(MouseEvent arg0) {
			}
			@Override
			public void mouseExited(MouseEvent arg0) {
			}
			@Override
			public void mouseEntered(MouseEvent arg0) {
			}
			@Override
			public void mouseClicked(MouseEvent evt) {
				 @SuppressWarnings("unused")
				JList<?> list = (JList<?>)evt.getSource();
			        if (evt.getClickCount() == 2) {
			        	Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
			            if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
			                try {
			                	URI test = new URI(listeURL.getSelectedValue());
			                    desktop.browse(test);
			                } catch (Exception e) {
			                    e.printStackTrace();
			                }
			            }
			        } 			
		        }
			});
		
		listeURL.setVisibleRowCount(5);
		
		JScrollPane panelListImg = new JScrollPane(listeURL);
		this.add(panelListImg, gc);
		///
		//MODE NAVIGATION
		gc.weighty = 0.1;
		
		gc.gridwidth = 4;
		gc.gridheight = 1;
		gc.gridx=0;
		gc.gridy=8;
		JButton modeNav = new JButton("Navigation parcours");
		modeNav.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {	
				parent.setAddMode(false);
				parent.setEditMode(false);
				parent.setNavigationMode(true);
				parent.reload();
			}
		});
		this.add(modeNav, gc);
		
		//ETAGE BTN
		//EDIT
		gc.weighty = 1;
		gc.gridwidth = 2;
		gc.gridheight = 1;
		gc.gridx=0;
		gc.gridy=9;
		JButton modeEdition = new JButton("Mode edition");
		modeEdition.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				parent.EditMode(true);
				parent.reload();
			}
		});
		this.add(modeEdition, gc);
		//DEL
		gc.gridwidth = 2;
		gc.gridheight = 1;
		gc.gridx=2;
		gc.gridy=9;
		JButton btnDelete = new JButton("Supprimer");
		btnDelete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				 switch (parent.getMode()) {
				    case ITINERARY:
				    	Itinerary newItinerary = (Itinerary) element;				    	
				        parent.getServiceItinerary().removeItinerary(newItinerary);
				     break;
				    case PLACE:
				    	Place newPlace = (Place) element;				    	
				        //parent.getServicePlace().removePlace(newPlace);
				     break;
				    case NEWS:
				    	News newNews = (News) element;				    	
				        //parent.getServiceNews().removeNews(newNews);
				    default: 
				     break;
				    }
				//parent.reloadResult();
			}
		});
		this.add(btnDelete, gc);
		this.revalidate();
	}
	public void fillPanelMap(){
		this.removeAll();
		this.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
		this.setLayout(new GridBagLayout());
		GridBagConstraints gc = new GridBagConstraints();
		gc.fill=GridBagConstraints.BOTH;
		//PARAMETER
		gc.weightx=1;
		gc.weighty=1;
		//---------------------------------------//
		//LABEL
		gc.gridheight=1;
		gc.gridwidth= 4;
		gc.gridx=0;
		gc.gridy=0;
		gc.weighty = 0.3;
		gc.insets = new Insets(0, 0, 5, 0);
		JPanel panelTitre = new JPanel(new BorderLayout());
		JLabel titre = new JLabel(element.getLabel());
		
		panelTitre.setBackground(Color.white);
		titre.setBackground(Color.white);
		titre.setOpaque(true);
		
		
		titre.setVerticalAlignment(SwingConstants.CENTER);
		titre.setHorizontalAlignment(SwingConstants.CENTER);
		panelTitre.add(titre, BorderLayout.CENTER);
		this.add(panelTitre, gc);
		//ETAGE DESCRIPTION
		gc.weighty = 3;
		gc.insets = new Insets(3, 5, 3, 5);
		gc.gridwidth = 4;
		gc.gridheight = 1;
		gc.gridx=0;
		gc.gridy=1;
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		JLabel description = new JLabel(element.getText());
		JScrollPane scroll = new JScrollPane(description);
		panel.add(scroll, BorderLayout.CENTER);
		this.add(panel, gc);
		//CHARGER CARTE
		gc.weighty = 0.5;
		gc.insets = new Insets(3, 5, 3, 5);
		gc.gridwidth = 4;
		gc.gridheight = 1;
		gc.gridx=0;
		gc.gridy=2;
		JButton load = new JButton("Charger carte");
		load.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				GUIManager.getInstance().runNavigation((Place)element);
			}
		});
		this.add(load, gc);
		
		//ETAGE BTN
		//EDIT
		gc.gridwidth = 2;
		gc.gridheight = 1;
		gc.gridx=0;
		gc.gridy=3;
		JButton modeEdition = new JButton("Mode edition");
		modeEdition.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				parent.EditMode(true);
				parent.reload();
			}
		});
		this.add(modeEdition, gc);
		//DEL
		gc.gridwidth = 2;
		gc.gridheight = 1;
		gc.gridx=2;
		gc.gridy=3;
		JButton btnDelete = new JButton("Supprimer");
			btnDelete.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				 switch (parent.getMode()) {
				    case PLACE:
				    	Place newPlace = (Place) element;    	
				        //parent.getServicePlace().removePlace(newPlace);
				    default: 
				     break;
				    }
				//parent.reloadResult();
			}
		});
		this.add(btnDelete, gc);
		this.revalidate();
	}
}
